/**
 * 
 */
package krush.rezepte;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.jar.Attributes.Name;
import strukturen.Kategorie;
import strukturen.Rezept;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class RezeptView extends Activity
{
    private Rezept rezept;
    private final int NAME_BEARBEITEN = 0;
    private final int BEMERKUNG_BEARBEITEN = 1;
    private final int SUCHBEGRIFFE_BEARBEITEN = 2;
    private final int KATEGORIE_AENDERN = 3;
    

    /*
     * private static final FrameLayout.LayoutParams ZOOM_PARAMS = new
     * FrameLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT,
     * ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
     */

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.rezept_view);	
	Bundle extras = getIntent().getExtras();
	long rid = extras.getLong("rid");
	DBHelper db = new DBHelper(this);
	rezept = db.getRezept(rid);
	init();
	

    }
    
    private void init()
    {
	DBHelper db = new DBHelper(this);
	setTitle(rezept.getName() + "(" + db.getKategorie(rezept.getKat_id()) + ")");
	((TextView) findViewById(R.id.rv_RezeptBemerkung_Text)).setText(rezept
		.getBemerkung());

	File file = new File(rezept.getLink());

	InputStream is;
	try
	{
	    is = new FileInputStream(file);
	    Bitmap bmp = BitmapFactory.decodeStream(is);
	    ((ImageView) findViewById(R.id.rv_bild)).setImageBitmap(bmp);
	    ((LinearLayout) findViewById(R.id.rv_BildLayout))
		    .setVisibility(View.VISIBLE);
	    ((LinearLayout) findViewById(R.id.rv_VerweisLayout))
		    .setVisibility(View.GONE);
	}
	catch (FileNotFoundException e)
	{
	    Log.e("krush2", "FNF" + file.getPath());
	    ((LinearLayout) findViewById(R.id.rv_BildLayout))
		    .setVisibility(View.GONE);
	    ((LinearLayout) findViewById(R.id.rv_VerweisLayout))
		    .setVisibility(View.VISIBLE);
	    ((TextView) findViewById(R.id.rv_VerweisText_Text)).setText(rezept
		    .getLink());
	    e.printStackTrace();
	}

	/*
	 * Für WebView String fileName = rezept.getLink(); WebView full =
	 * ((WebView)findViewById(R.id.wv));
	 * 
	 * FrameLayout mContentView = (FrameLayout)
	 * getWindow().getDecorView().findViewById(android.R.id.content); final
	 * View zoom = full.getZoomControls();
	 * 
	 * mContentView.addView(zoom, ZOOM_PARAMS);
	 * zoom.setVisibility(View.VISIBLE);
	 * 
	 * String html = new String(); html =
	 * "<html><center><img src="+fileName+" width=\"100%\"></html>" ;
	 * full.loadDataWithBaseURL
	 * ("file:///sdcard/data/data/com.krush.rezepte/", html, "text/html",
	 * "utf-8", "");
	 */
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        menu.add(0, NAME_BEARBEITEN, NAME_BEARBEITEN, "Namen bearbeiten");
        menu.add(0, BEMERKUNG_BEARBEITEN, BEMERKUNG_BEARBEITEN, "Bemerkung bearbeiten");
        menu.add(0, SUCHBEGRIFFE_BEARBEITEN, SUCHBEGRIFFE_BEARBEITEN, "Suchbegriffe bearbeiten");
        menu.add(0, KATEGORIE_AENDERN, KATEGORIE_AENDERN, "Kategorie ändern");
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item)
    {
	final DBHelper db = new DBHelper(this);
	
	if (item.getItemId() == NAME_BEARBEITEN)
	{
	    final AlertDialog.Builder alert = new AlertDialog.Builder(this);
	    final EditText input = new EditText(this);
	    input.setText(rezept.getName());
	    alert.setView(input);
	    alert.setTitle("Rezeptnamen bearbeiten");
	    alert.setCancelable(false);
	    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
	    {
		public void onClick(DialogInterface dialog, int whichButton)
		{
		    String value = input.getText().toString().trim();
		    db.updateRezeptName(rezept.getId(), value);
		    rezept = db.getRezept(rezept.getId());
		    init();
		}
	    });

	    alert.setNegativeButton("Abbrechen",
		    new DialogInterface.OnClickListener()
		    {
			public void onClick(DialogInterface dialog,
				int whichButton)
			{
			    dialog.cancel();
			}
		    });
	    alert.show();
	}
	else if (item.getItemId() == BEMERKUNG_BEARBEITEN)
	{
	    final AlertDialog.Builder alert = new AlertDialog.Builder(this);
	    final EditText input = new EditText(this);
	    input.setLines(4);
	    input.setText(rezept.getBemerkung());
	    alert.setView(input);
	    alert.setTitle("Bemerkung bearbeiten");
	    alert.setCancelable(false);
	    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
	    {
		public void onClick(DialogInterface dialog, int whichButton)
		{
		    String value = input.getText().toString().trim();
		    db.updateRezeptBemerkung(rezept.getId(), value);
		    rezept = db.getRezept(rezept.getId());
		    init();
		}
	    });

	    alert.setNegativeButton("Abbrechen",
		    new DialogInterface.OnClickListener()
		    {
			public void onClick(DialogInterface dialog,
				int whichButton)
			{
			    dialog.cancel();
			}
		    });
	    alert.show();
	}
	else if (item.getItemId() == SUCHBEGRIFFE_BEARBEITEN)
	{
	    final AlertDialog.Builder alert = new AlertDialog.Builder(this);
	    final EditText input = new EditText(this);
	    input.setText(rezept.getSuchbegriffe());
	    alert.setView(input);
	    alert.setTitle("Suchbegriffe bearbeiten");
	    alert.setCancelable(false);
	    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
	    {
		public void onClick(DialogInterface dialog, int whichButton)
		{
		    String value = input.getText().toString().trim();
		    db.deleteSuchbegriffeForRezept(rezept.getId());
		    db.insertSuchbegriffe(value, rezept.getId());
		}
	    });

	    alert.setNegativeButton("Abbrechen",
		    new DialogInterface.OnClickListener()
		    {
			public void onClick(DialogInterface dialog,
				int whichButton)
			{
			    dialog.cancel();
			}
		    });
	    alert.show();
	}
	else if(item.getItemId() == KATEGORIE_AENDERN)
	{
	    final ArrayList<Kategorie> kategorien = db.getKategorien();
	    final String[] items = db.getKategorienAsStringArrayOhneLeerstelle();

	    AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    builder.setTitle("Kategorie wählen");
	    builder.setItems(items, new DialogInterface.OnClickListener() {
	        public void onClick(DialogInterface dialog, int item) 
	        {
	            db.updateRezeptKategorie(rezept.getId(), kategorien.get(item).getId());
		    rezept = db.getRezept(rezept.getId());
		    init();
	            
	        }
	    });
	    AlertDialog alert = builder.create();
	    alert.show();
	}

	return super.onMenuItemSelected(featureId, item);
    }
}
